package com.wiz.weixin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wiz.weixin.TO.StationTO;
import com.wiz.weixin.TO.VerificationCodeTO;
import com.wiz.weixin.VO.AmountHistoryVO;
import com.wiz.weixin.VO.MemberVO;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.mapper.StationMapper;
import com.wiz.weixin.mapper.VerificationCodeMapper;
import com.wiz.weixin.service.MemberService;
import com.wiz.weixin.util.PageView;
@Service("memberService")
public class MemberServiceImpl implements MemberService {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private HttpManager httpManager;
	@Resource
	private StationMapper stationMapper;
	@Resource
	private VerificationCodeMapper verificationCodeMapper;
	
	@Override
	public String register(MemberVO member,int code)throws Exception {
		List<StationTO>list = stationMapper.getStation(member.getStationId());
		if(list == null || list.size()<0){
			throw new Exception("没有找到当前站点");
		}
		StationTO stationTO = list.get(0);
		
		List<VerificationCodeTO>codes = verificationCodeMapper.getCode(member.getPhoneNum(), code);
		if(codes == null || codes.size()<=0){
			new Exception("验证码失效");
		}
		//查询会员信息
		Map<String,String> params = new HashMap<String,String>();
		
		params.put("HQId", stationTO.getHqId());
		params.put("HQName", stationTO.getHqName());
		params.put("stationId", stationTO.getStationId());
		params.put("stationName", stationTO.getStationName());
		params.put("memberName", member.getName());
		params.put("fuellingPw", member.getFuellingPw());
		params.put("licensePlate", member.getLicensePlate());
		params.put("phoneNum", member.getPhoneNum());
		params.put("idCard", member.getIdCard());
		params.put("openId", member.getOpenId());
		params.put("sourceType", "3");
		String password =  member.getFuellingPw();
		if(password != null && !password.equals("")){
			params.put("isCheckFuellingPw", "1");
		}
		
    	String response = httpManager.getHttpsResponseByPost(Constant.ADD_MEMBER_URL, params, "utf-8");
    	Map<String,String> map = gson.fromJson(response,new TypeToken<Map<String,String>>() {}.getType());
    	code =  Integer.valueOf(map.get("code"));
    	if(code != 0){
    		throw new Exception(map.get("msg"));
    	}
    	
    	return response;
	}
	/**
	 * 账户余额
	 * 
	 */
	@Override
	public String amounts(String openId)throws Exception{
		List<MemberVO>list = this.listMemberByOpenId(openId);
		StringBuffer buffer = new StringBuffer();
		buffer.append("我的账户").append("\n\n\n");
		for (MemberVO memberVO : list) {
			buffer.append(memberVO.getHQName()).append("\n");
			buffer.append("账户余额："+memberVO.getAmount()+"元").append("\n\n");
		}
		return buffer.toString();
		
	}
	/**
	 * 根据openId得到客户下的所有账户
	 */
	@Override
	public List<MemberVO> listMemberByOpenId(String openId) throws Exception {
		List<MemberVO>list = new ArrayList<MemberVO>();
		Map<String,String> params = new HashMap<String,String>();
		params.put("openId", openId);
		String result = httpManager.getHttpsResponseByPost(Constant.QUERY_MEMBER_URL, params, "utf-8");
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("0")){
				JSONObject data = json.getJSONObject("data");
				String str = data.getString("list");
				list = gson.fromJson(str,new TypeToken<List<MemberVO>>(){}.getType());
				
			}
		}
		return list;
	}
	@Override
	public PageView listAmountHistory(String hqId,String memberId, PageView pageView) throws Exception {
		List<AmountHistoryVO>list = new ArrayList<AmountHistoryVO>();
		Map<String,String> params = new HashMap<String,String>();
		params.put("HQId", hqId);
		params.put("memberId", memberId);
		params.put("memberId", memberId);
		params.put("pageView", String.valueOf(pageView.getPageNow()));
		params.put("ps", String.valueOf(pageView.getPageSize()));
		String result = httpManager.getHttpsResponseByPost(Constant.LIST_AMOUNT_HISTORY, params, "utf-8");
		System.out.println(result);
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("0")){
				JSONObject data = json.getJSONObject("data");
				String str = data.getString("list");
				list = gson.fromJson(str,new TypeToken<List<AmountHistoryVO>>(){}.getType());
				pageView.setRowCount(data.getInt("recordCnt"));
				pageView.setRecords(list);
			}
		}
		return pageView;
	}

}
